//
//  NineteenViewController.m
//  项目二
//
//  Created by rimi on 16/5/17.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "NineteenViewController.h"
#import "NSUserDefaultsViewController.h"
@interface NineteenViewController ()
@property (nonatomic,strong)UITextField *text1;
@property (nonatomic,strong)UITextField *text2;
@property (nonatomic,strong)NSString *path;
@end

@implementation NineteenViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"归档";
    [self initUIInterface];
}

-(void)initUIInterface{
    _text1 = [[UITextField alloc]initWithFrame:CRM(100, 100, 100, 40)];
    _text1.placeholder = @"输入分组名";
    _text1.borderStyle = UITextBorderStyleRoundedRect;
    _text1.backgroundColor = [UIColor redColor];
    [self.view addSubview:_text1];
    
    _text2 = [[UITextField alloc]initWithFrame:CRM(100, 180, 100, 40)];
    _text2.placeholder = @"解档显示";
    _text2.borderStyle = UITextBorderStyleRoundedRect;
    _text2.backgroundColor = [UIColor redColor];
    [self.view addSubview:_text2];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CRM(100, 250, 70, 50);
    [button1 setTitle:@"归档" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor orangeColor];
    [button1 addTarget:self action:@selector(button1T:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CRM(250, 250, 70, 50);
    [button2 setTitle:@"解档" forState:UIControlStateNormal];
    button2.backgroundColor = [UIColor orangeColor];
    [button2 addTarget:self action:@selector(button2T:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CRM( 300, 300, 80, 50);
    [button3 setTitle:@"下一页" forState:UIControlStateNormal];
    button3.backgroundColor = [UIColor orangeColor];
    [button3 addTarget:self action:@selector(button3T:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
}

#pragma mark - Button
-(void)button1T:(UIButton *)sender{
      /**获取文件路径*/
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    /**添加文件存储名*/
    NSString *path = [docPath stringByAppendingPathComponent:@"person.zhuzhu"];
    NSLog(@"path= %@",path);
    /**归档*/
    [NSKeyedArchiver archiveRootObject:_text1.text toFile:path];
}

-(void)button2T:(UIButton *)sender{
    /**获取文件路径*/
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path = [docPath stringByAppendingPathComponent:@"person.zhuzhu"];
    _text2.text = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"%@",_text2.text);
}

-(void)button3T:(UIButton *)sender{
    NSUserDefaultsViewController *NSUser = [[NSUserDefaultsViewController alloc]init];
    [self.navigationController pushViewController:NSUser animated:YES];
}

#pragma mark -- NSCopying,NSCoding
//将属性进行编码
-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:_text1.text forKey:@"1"];
    [aCoder encodeObject:_text2.text forKey:@"2"];
}
@end
